home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 October / maximum-cd-2009-10.iso / DiscContents / Firefox Setup 3.5.exe / nonlocalized / chrome / browser.jar / content / browser / preferences / applications.js < prev    next >
Encoding:
JavaScript  |  2009-06-24  |  67.5 KB  |  1,825 lines

  1. /*
  2. //@line 44 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  3.  */
  4.  
  5. //****************************************************************************//
  6. // Constants & Enumeration Values
  7.  
  8. /*
  9. //@line 51 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  10. */
  11. var Cc = Components.classes;
  12. var Ci = Components.interfaces;
  13. var Cr = Components.results;
  14. /*
  15. //@line 57 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  16. */
  17.  
  18. const TYPE_MAYBE_FEED = "application/vnd.mozilla.maybe.feed";
  19. const TYPE_MAYBE_VIDEO_FEED = "application/vnd.mozilla.maybe.video.feed";
  20. const TYPE_MAYBE_AUDIO_FEED = "application/vnd.mozilla.maybe.audio.feed";
  21.  
  22. const PREF_DISABLED_PLUGIN_TYPES = "plugin.disable_full_page_plugin_for_types";
  23.  
  24. // Preferences that affect which entries to show in the list.
  25. const PREF_SHOW_PLUGINS_IN_LIST = "browser.download.show_plugins_in_list";
  26. const PREF_HIDE_PLUGINS_WITHOUT_EXTENSIONS =
  27.   "browser.download.hide_plugins_without_extensions";
  28.  
  29. /*
  30.  * Preferences where we store handling information about the feed type.
  31.  *
  32.  * browser.feeds.handler
  33.  * - "bookmarks", "reader" (clarified further using the .default preference),
  34.  *   or "ask" -- indicates the default handler being used to process feeds;
  35.  *   "bookmarks" is obsolete; to specify that the handler is bookmarks,
  36.  *   set browser.feeds.handler.default to "bookmarks";
  37.  *
  38.  * browser.feeds.handler.default
  39.  * - "bookmarks", "client" or "web" -- indicates the chosen feed reader used
  40.  *   to display feeds, either transiently (i.e., when the "use as default"
  41.  *   checkbox is unchecked, corresponds to when browser.feeds.handler=="ask")
  42.  *   or more permanently (i.e., the item displayed in the dropdown in Feeds
  43.  *   preferences)
  44.  *
  45.  * browser.feeds.handler.webservice
  46.  * - the URL of the currently selected web service used to read feeds
  47.  *
  48.  * browser.feeds.handlers.application
  49.  * - nsILocalFile, stores the current client-side feed reading app if one has
  50.  *   been chosen
  51.  */
  52. const PREF_FEED_SELECTED_APP    = "browser.feeds.handlers.application";
  53. const PREF_FEED_SELECTED_WEB    = "browser.feeds.handlers.webservice";
  54. const PREF_FEED_SELECTED_ACTION = "browser.feeds.handler";
  55. const PREF_FEED_SELECTED_READER = "browser.feeds.handler.default";
  56.  
  57. const PREF_VIDEO_FEED_SELECTED_APP    = "browser.videoFeeds.handlers.application";
  58. const PREF_VIDEO_FEED_SELECTED_WEB    = "browser.videoFeeds.handlers.webservice";
  59. const PREF_VIDEO_FEED_SELECTED_ACTION = "browser.videoFeeds.handler";
  60. const PREF_VIDEO_FEED_SELECTED_READER = "browser.videoFeeds.handler.default";
  61.  
  62. const PREF_AUDIO_FEED_SELECTED_APP    = "browser.audioFeeds.handlers.application";
  63. const PREF_AUDIO_FEED_SELECTED_WEB    = "browser.audioFeeds.handlers.webservice";
  64. const PREF_AUDIO_FEED_SELECTED_ACTION = "browser.audioFeeds.handler";
  65. const PREF_AUDIO_FEED_SELECTED_READER = "browser.audioFeeds.handler.default";
  66.  
  67. // The nsHandlerInfoAction enumeration values in nsIHandlerInfo identify
  68. // the actions the application can take with content of various types.
  69. // But since nsIHandlerInfo doesn't support plugins, there's no value
  70. // identifying the "use plugin" action, so we use this constant instead.
  71. const kActionUsePlugin = 5;
  72.  
  73. /*
  74. //@line 120 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  75. */
  76. const ICON_URL_APP      = "chrome://browser/skin/preferences/application.png";
  77. /*
  78. //@line 124 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  79. */
  80.  
  81. // For CSS. Can be one of "ask", "save", "plugin" or "feed". If absent, the icon URL
  82. // was set by us to a custom handler icon and CSS should not try to override it.
  83. const APP_ICON_ATTR_NAME = "appHandlerIcon";
  84.  
  85. //****************************************************************************//
  86. // Utilities
  87.  
  88. function getDisplayNameForFile(aFile) {
  89. /*
  90. //@line 136 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  91. */
  92.   if (aFile instanceof Ci.nsILocalFileWin) {
  93.     try {
  94.       return aFile.getVersionInfoField("FileDescription"); 
  95.     }
  96.     catch(ex) {
  97.       // fall through to the file name
  98.     }
  99.   }
  100. /*
  101. //@line 159 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  102. */
  103.  
  104.   return Cc["@mozilla.org/network/io-service;1"].
  105.          getService(Ci.nsIIOService).
  106.          newFileURI(aFile).
  107.          QueryInterface(Ci.nsIURL).
  108.          fileName;
  109. }
  110.  
  111. function getLocalHandlerApp(aFile) {
  112.   var localHandlerApp = Cc["@mozilla.org/uriloader/local-handler-app;1"].
  113.                         createInstance(Ci.nsILocalHandlerApp);
  114.   localHandlerApp.name = getDisplayNameForFile(aFile);
  115.   localHandlerApp.executable = aFile;
  116.  
  117.   return localHandlerApp;
  118. }
  119.  
  120. /**
  121.  * An enumeration of items in a JS array.
  122.  *
  123.  * FIXME: use ArrayConverter once it lands (bug 380839).
  124.  * 
  125.  * @constructor
  126.  */
  127. function ArrayEnumerator(aItems) {
  128.   this._index = 0;
  129.   this._contents = aItems;
  130. }
  131.  
  132. ArrayEnumerator.prototype = {
  133.   _index: 0,
  134.  
  135.   hasMoreElements: function() {
  136.     return this._index < this._contents.length;
  137.   },
  138.  
  139.   getNext: function() {
  140.     return this._contents[this._index++];
  141.   }
  142. };
  143.  
  144. function isFeedType(t) {
  145.   return t == TYPE_MAYBE_FEED || t == TYPE_MAYBE_VIDEO_FEED || t == TYPE_MAYBE_AUDIO_FEED;
  146. }
  147.  
  148. //****************************************************************************//
  149. // HandlerInfoWrapper
  150.  
  151. /**
  152.  * This object wraps nsIHandlerInfo with some additional functionality
  153.  * the Applications prefpane needs to display and allow modification of
  154.  * the list of handled types.
  155.  * 
  156.  * We create an instance of this wrapper for each entry we might display
  157.  * in the prefpane, and we compose the instances from various sources,
  158.  * including navigator.plugins and the handler service.
  159.  *
  160.  * We don't implement all the original nsIHandlerInfo functionality,
  161.  * just the stuff that the prefpane needs.
  162.  * 
  163.  * In theory, all of the custom functionality in this wrapper should get
  164.  * pushed down into nsIHandlerInfo eventually.
  165.  */
  166. function HandlerInfoWrapper(aType, aHandlerInfo) {
  167.   this._type = aType;
  168.   this.wrappedHandlerInfo = aHandlerInfo;
  169. }
  170.  
  171. HandlerInfoWrapper.prototype = {
  172.   // The wrapped nsIHandlerInfo object.  In general, this object is private,
  173.   // but there are a couple cases where callers access it directly for things
  174.   // we haven't (yet?) implemented, so we make it a public property.
  175.   wrappedHandlerInfo: null,
  176.  
  177.  
  178.   //**************************************************************************//
  179.   // Convenience Utils
  180.  
  181.   _handlerSvc: Cc["@mozilla.org/uriloader/handler-service;1"].
  182.                getService(Ci.nsIHandlerService),
  183.  
  184.   // Retrieve this as nsIPrefBranch and then immediately QI to nsIPrefBranch2
  185.   // so both interfaces are available to callers.
  186.   _prefSvc: Cc["@mozilla.org/preferences-service;1"].
  187.             getService(Ci.nsIPrefBranch).
  188.             QueryInterface(Ci.nsIPrefBranch2),
  189.  
  190.   _categoryMgr: Cc["@mozilla.org/categorymanager;1"].
  191.                 getService(Ci.nsICategoryManager),
  192.  
  193.   element: function(aID) {
  194.     return document.getElementById(aID);
  195.   },
  196.  
  197.  
  198.   //**************************************************************************//
  199.   // nsIHandlerInfo
  200.  
  201.   // The MIME type or protocol scheme.
  202.   _type: null,
  203.   get type() {
  204.     return this._type;
  205.   },
  206.  
  207.   get description() {
  208.     if (this.wrappedHandlerInfo.description)
  209.       return this.wrappedHandlerInfo.description;
  210.  
  211.     if (this.primaryExtension) {
  212.       var extension = this.primaryExtension.toUpperCase();
  213.       return this.element("bundlePreferences").getFormattedString("fileEnding",
  214.                                                                   [extension]);
  215.     }
  216.  
  217.     return this.type;
  218.   },
  219.  
  220.   get preferredApplicationHandler() {
  221.     return this.wrappedHandlerInfo.preferredApplicationHandler;
  222.   },
  223.  
  224.   set preferredApplicationHandler(aNewValue) {
  225.     this.wrappedHandlerInfo.preferredApplicationHandler = aNewValue;
  226.  
  227.     // Make sure the preferred handler is in the set of possible handlers.
  228.     if (aNewValue)
  229.       this.addPossibleApplicationHandler(aNewValue)
  230.   },
  231.  
  232.   get possibleApplicationHandlers() {
  233.     return this.wrappedHandlerInfo.possibleApplicationHandlers;
  234.   },
  235.  
  236.   addPossibleApplicationHandler: function(aNewHandler) {
  237.     var possibleApps = this.possibleApplicationHandlers.enumerate();
  238.     while (possibleApps.hasMoreElements()) {
  239.       if (possibleApps.getNext().equals(aNewHandler))
  240.         return;
  241.     }
  242.     this.possibleApplicationHandlers.appendElement(aNewHandler, false);
  243.   },
  244.  
  245.   removePossibleApplicationHandler: function(aHandler) {
  246.     var defaultApp = this.preferredApplicationHandler;
  247.     if (defaultApp && aHandler.equals(defaultApp)) {
  248.       // If the app we remove was the default app, we must make sure
  249.       // it won't be used anymore
  250.       this.alwaysAskBeforeHandling = true;
  251.       this.preferredApplicationHandler = null;
  252.     }
  253.  
  254.     var handlers = this.possibleApplicationHandlers;
  255.     for (var i = 0; i < handlers.length; ++i) {
  256.       var handler = handlers.queryElementAt(i, Ci.nsIHandlerApp);
  257.       if (handler.equals(aHandler)) {
  258.         handlers.removeElementAt(i);
  259.         break;
  260.       }
  261.     }
  262.   },
  263.  
  264.   get hasDefaultHandler() {
  265.     return this.wrappedHandlerInfo.hasDefaultHandler;
  266.   },
  267.  
  268.   get defaultDescription() {
  269.     return this.wrappedHandlerInfo.defaultDescription;
  270.   },
  271.  
  272.   // What to do with content of this type.
  273.   get preferredAction() {
  274.     // If we have an enabled plugin, then the action is to use that plugin.
  275.     if (this.plugin && !this.isDisabledPluginType)
  276.       return kActionUsePlugin;
  277.  
  278.     // If the action is to use a helper app, but we don't have a preferred
  279.     // handler app, then switch to using the system default, if any; otherwise
  280.     // fall back to saving to disk, which is the default action in nsMIMEInfo.
  281.     // Note: "save to disk" is an invalid value for protocol info objects,
  282.     // but the alwaysAskBeforeHandling getter will detect that situation
  283.     // and always return true in that case to override this invalid value.
  284.     if (this.wrappedHandlerInfo.preferredAction == Ci.nsIHandlerInfo.useHelperApp &&
  285.         !gApplicationsPane.isValidHandlerApp(this.preferredApplicationHandler)) {
  286.       if (this.wrappedHandlerInfo.hasDefaultHandler)
  287.         return Ci.nsIHandlerInfo.useSystemDefault;
  288.       else
  289.         return Ci.nsIHandlerInfo.saveToDisk;
  290.     }
  291.  
  292.     return this.wrappedHandlerInfo.preferredAction;
  293.   },
  294.  
  295.   set preferredAction(aNewValue) {
  296.     // We don't modify the preferred action if the new action is to use a plugin
  297.     // because handler info objects don't understand our custom "use plugin"
  298.     // value.  Also, leaving it untouched means that we can automatically revert
  299.     // to the old setting if the user ever removes the plugin.
  300.  
  301.     if (aNewValue != kActionUsePlugin)
  302.       this.wrappedHandlerInfo.preferredAction = aNewValue;
  303.   },
  304.  
  305.   get alwaysAskBeforeHandling() {
  306.     // If this type is handled only by a plugin, we can't trust the value
  307.     // in the handler info object, since it'll be a default based on the absence
  308.     // of any user configuration, and the default in that case is to always ask,
  309.     // even though we never ask for content handled by a plugin, so special case
  310.     // plugin-handled types by returning false here.
  311.     if (this.plugin && this.handledOnlyByPlugin)
  312.       return false;
  313.  
  314.     // If this is a protocol type and the preferred action is "save to disk",
  315.     // which is invalid for such types, then return true here to override that
  316.     // action.  This could happen when the preferred action is to use a helper
  317.     // app, but the preferredApplicationHandler is invalid, and there isn't
  318.     // a default handler, so the preferredAction getter returns save to disk
  319.     // instead.
  320.     if (!(this.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo) &&
  321.         this.preferredAction == Ci.nsIHandlerInfo.saveToDisk)
  322.       return true;
  323.  
  324.     return this.wrappedHandlerInfo.alwaysAskBeforeHandling;
  325.   },
  326.  
  327.   set alwaysAskBeforeHandling(aNewValue) {
  328.     this.wrappedHandlerInfo.alwaysAskBeforeHandling = aNewValue;
  329.   },
  330.  
  331.  
  332.   //**************************************************************************//
  333.   // nsIMIMEInfo
  334.  
  335.   // The primary file extension associated with this type, if any.
  336.   //
  337.   // XXX Plugin objects contain an array of MimeType objects with "suffixes"
  338.   // properties; if this object has an associated plugin, shouldn't we check
  339.   // those properties for an extension?
  340.   get primaryExtension() {
  341.     try {
  342.       if (this.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo &&
  343.           this.wrappedHandlerInfo.primaryExtension)
  344.         return this.wrappedHandlerInfo.primaryExtension
  345.     } catch(ex) {}
  346.  
  347.     return null;
  348.   },
  349.  
  350.  
  351.   //**************************************************************************//
  352.   // Plugin Handling
  353.  
  354.   // A plugin that can handle this type, if any.
  355.   //
  356.   // Note: just because we have one doesn't mean it *will* handle the type.
  357.   // That depends on whether or not the type is in the list of types for which
  358.   // plugin handling is disabled.
  359.   plugin: null,
  360.  
  361.   // Whether or not this type is only handled by a plugin or is also handled
  362.   // by some user-configured action as specified in the handler info object.
  363.   //
  364.   // Note: we can't just check if there's a handler info object for this type,
  365.   // because OS and user configuration is mixed up in the handler info object,
  366.   // so we always need to retrieve it for the OS info and can't tell whether
  367.   // it represents only OS-default information or user-configured information.
  368.   //
  369.   // FIXME: once handler info records are broken up into OS-provided records
  370.   // and user-configured records, stop using this boolean flag and simply
  371.   // check for the presence of a user-configured record to determine whether
  372.   // or not this type is only handled by a plugin.  Filed as bug 395142.
  373.   handledOnlyByPlugin: undefined,
  374.  
  375.   get isDisabledPluginType() {
  376.     return this._getDisabledPluginTypes().indexOf(this.type) != -1;
  377.   },
  378.  
  379.   _getDisabledPluginTypes: function() {
  380.     var types = "";
  381.  
  382.     if (this._prefSvc.prefHasUserValue(PREF_DISABLED_PLUGIN_TYPES))
  383.       types = this._prefSvc.getCharPref(PREF_DISABLED_PLUGIN_TYPES);
  384.  
  385.     // Only split if the string isn't empty so we don't end up with an array
  386.     // containing a single empty string.
  387.     if (types != "")
  388.       return types.split(",");
  389.  
  390.     return [];
  391.   },
  392.  
  393.   disablePluginType: function() {
  394.     var disabledPluginTypes = this._getDisabledPluginTypes();
  395.  
  396.     if (disabledPluginTypes.indexOf(this.type) == -1)
  397.       disabledPluginTypes.push(this.type);
  398.  
  399.     this._prefSvc.setCharPref(PREF_DISABLED_PLUGIN_TYPES,
  400.                               disabledPluginTypes.join(","));
  401.  
  402.     // Update the category manager so existing browser windows update.
  403.     this._categoryMgr.deleteCategoryEntry("Gecko-Content-Viewers",
  404.                                           this.type,
  405.                                           false);
  406.   },
  407.  
  408.   enablePluginType: function() {
  409.     var disabledPluginTypes = this._getDisabledPluginTypes();
  410.  
  411.     var type = this.type;
  412.     disabledPluginTypes = disabledPluginTypes.filter(function(v) v != type);
  413.  
  414.     this._prefSvc.setCharPref(PREF_DISABLED_PLUGIN_TYPES,
  415.                               disabledPluginTypes.join(","));
  416.  
  417.     // Update the category manager so existing browser windows update.
  418.     this._categoryMgr.
  419.       addCategoryEntry("Gecko-Content-Viewers",
  420.                        this.type,
  421.                        "@mozilla.org/content/plugin/document-loader-factory;1",
  422.                        false,
  423.                        true);
  424.   },
  425.  
  426.  
  427.   //**************************************************************************//
  428.   // Storage
  429.  
  430.   store: function() {
  431.     this._handlerSvc.store(this.wrappedHandlerInfo);
  432.   },
  433.  
  434.  
  435.   //**************************************************************************//
  436.   // Icons
  437.  
  438.   get smallIcon() {
  439.     return this._getIcon(16);
  440.   },
  441.  
  442.   get largeIcon() {
  443.     return this._getIcon(32);
  444.   },
  445.  
  446.   _getIcon: function(aSize) {
  447.     if (this.primaryExtension)
  448.       return "moz-icon://goat." + this.primaryExtension + "?size=" + aSize;
  449.  
  450.     if (this.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo)
  451.       return "moz-icon://goat?size=" + aSize + "&contentType=" + this.type;
  452.  
  453.     // FIXME: consider returning some generic icon when we can't get a URL for
  454.     // one (for example in the case of protocol schemes).  Filed as bug 395141.
  455.     return null;
  456.   }
  457.  
  458. };
  459.  
  460.  
  461. //****************************************************************************//
  462. // Feed Handler Info
  463.  
  464. /**
  465.  * This object implements nsIHandlerInfo for the feed types.  It's a separate
  466.  * object because we currently store handling information for the feed type
  467.  * in a set of preferences rather than the nsIHandlerService-managed datastore.
  468.  * 
  469.  * This object inherits from HandlerInfoWrapper in order to get functionality
  470.  * that isn't special to the feed type.
  471.  * 
  472.  * XXX Should we inherit from HandlerInfoWrapper?  After all, we override
  473.  * most of that wrapper's properties and methods, and we have to dance around
  474.  * the fact that the wrapper expects to have a wrappedHandlerInfo, which we
  475.  * don't provide.
  476.  */
  477.  
  478. function FeedHandlerInfo(aMIMEType) {
  479.   HandlerInfoWrapper.call(this, aMIMEType, null);
  480. }
  481.  
  482. FeedHandlerInfo.prototype = {
  483.   __proto__: HandlerInfoWrapper.prototype,
  484.  
  485.   //**************************************************************************//
  486.   // Convenience Utils
  487.  
  488.   _converterSvc:
  489.     Cc["@mozilla.org/embeddor.implemented/web-content-handler-registrar;1"].
  490.     getService(Ci.nsIWebContentConverterService),
  491.  
  492.   _shellSvc:
  493. //@line 551 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  494.     getShellService(),
  495. //@line 555 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  496.  
  497.  
  498.   //**************************************************************************//
  499.   // nsIHandlerInfo
  500.  
  501.   get description() {
  502.     return this.element("bundlePreferences").getString(this._appPrefLabel);
  503.   },
  504.  
  505.   get preferredApplicationHandler() {
  506.     switch (this.element(this._prefSelectedReader).value) {
  507.       case "client":
  508.         var file = this.element(this._prefSelectedApp).value;
  509.         if (file)
  510.           return getLocalHandlerApp(file);
  511.  
  512.         return null;
  513.  
  514.       case "web":
  515.         var uri = this.element(this._prefSelectedWeb).value;
  516.         if (!uri)
  517.           return null;
  518.         return this._converterSvc.getWebContentHandlerByURI(this.type, uri);
  519.  
  520.       case "bookmarks":
  521.       default:
  522.         // When the pref is set to bookmarks, we handle feeds internally,
  523.         // we don't forward them to a local or web handler app, so there is
  524.         // no preferred handler.
  525.         return null;
  526.     }
  527.   },
  528.  
  529.   set preferredApplicationHandler(aNewValue) {
  530.     if (aNewValue instanceof Ci.nsILocalHandlerApp) {
  531.       this.element(this._prefSelectedApp).value = aNewValue.executable;
  532.       this.element(this._prefSelectedReader).value = "client";
  533.     }
  534.     else if (aNewValue instanceof Ci.nsIWebContentHandlerInfo) {
  535.       this.element(this._prefSelectedWeb).value = aNewValue.uri;
  536.       this.element(this._prefSelectedReader).value = "web";
  537.       // Make the web handler be the new "auto handler" for feeds.
  538.       // Note: we don't have to unregister the auto handler when the user picks
  539.       // a non-web handler (local app, Live Bookmarks, etc.) because the service
  540.       // only uses the "auto handler" when the selected reader is a web handler.
  541.       // We also don't have to unregister it when the user turns on "always ask"
  542.       // (i.e. preview in browser), since that also overrides the auto handler.
  543.       this._converterSvc.setAutoHandler(this.type, aNewValue);
  544.     }
  545.   },
  546.  
  547.   _possibleApplicationHandlers: null,
  548.  
  549.   get possibleApplicationHandlers() {
  550.     if (this._possibleApplicationHandlers)
  551.       return this._possibleApplicationHandlers;
  552.  
  553.     // A minimal implementation of nsIMutableArray.  It only supports the two
  554.     // methods its callers invoke, namely appendElement and nsIArray::enumerate.
  555.     this._possibleApplicationHandlers = {
  556.       _inner: [],
  557.       _removed: [],
  558.  
  559.       QueryInterface: function(aIID) {
  560.         if (aIID.equals(Ci.nsIMutableArray) ||
  561.             aIID.equals(Ci.nsIArray) ||
  562.             aIID.equals(Ci.nsISupports))
  563.           return this;
  564.  
  565.         throw Cr.NS_ERROR_NO_INTERFACE;
  566.       },
  567.  
  568.       get length() {
  569.         return this._inner.length;
  570.       },
  571.  
  572.       enumerate: function() {
  573.         return new ArrayEnumerator(this._inner);
  574.       },
  575.  
  576.       appendElement: function(aHandlerApp, aWeak) {
  577.         this._inner.push(aHandlerApp);
  578.       },
  579.  
  580.       removeElementAt: function(aIndex) {
  581.         this._removed.push(this._inner[aIndex]);
  582.         this._inner.splice(aIndex, 1);
  583.       },
  584.  
  585.       queryElementAt: function(aIndex, aInterface) {
  586.         return this._inner[aIndex].QueryInterface(aInterface);
  587.       }
  588.     };
  589.  
  590.     // Add the selected local app if it's different from the OS default handler.
  591.     // Unlike for other types, we can store only one local app at a time for the
  592.     // feed type, since we store it in a preference that historically stores
  593.     // only a single path.  But we display all the local apps the user chooses
  594.     // while the prefpane is open, only dropping the list when the user closes
  595.     // the prefpane, for maximum usability and consistency with other types.
  596.     var preferredAppFile = this.element(this._prefSelectedApp).value;
  597.     if (preferredAppFile) {
  598.       let preferredApp = getLocalHandlerApp(preferredAppFile);
  599.       let defaultApp = this._defaultApplicationHandler;
  600.       if (!defaultApp || !defaultApp.equals(preferredApp))
  601.         this._possibleApplicationHandlers.appendElement(preferredApp, false);
  602.     }
  603.  
  604.     // Add the registered web handlers.  There can be any number of these.
  605.     var webHandlers = this._converterSvc.getContentHandlers(this.type, {});
  606.     for each (let webHandler in webHandlers)
  607.       this._possibleApplicationHandlers.appendElement(webHandler, false);
  608.  
  609.     return this._possibleApplicationHandlers;
  610.   },
  611.  
  612.   __defaultApplicationHandler: undefined,
  613.   get _defaultApplicationHandler() {
  614.     if (typeof this.__defaultApplicationHandler != "undefined")
  615.       return this.__defaultApplicationHandler;
  616.  
  617.     var defaultFeedReader = null;
  618. //@line 678 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  619.     try {
  620.       defaultFeedReader = this._shellSvc.defaultFeedReader;
  621.     }
  622.     catch(ex) {
  623.       // no default reader or _shellSvc is null
  624.     }
  625. //@line 685 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  626.  
  627.     if (defaultFeedReader) {
  628.       let handlerApp = Cc["@mozilla.org/uriloader/local-handler-app;1"].
  629.                        createInstance(Ci.nsIHandlerApp);
  630.       handlerApp.name = getDisplayNameForFile(defaultFeedReader);
  631.       handlerApp.QueryInterface(Ci.nsILocalHandlerApp);
  632.       handlerApp.executable = defaultFeedReader;
  633.  
  634.       this.__defaultApplicationHandler = handlerApp;
  635.     }
  636.     else {
  637.       this.__defaultApplicationHandler = null;
  638.     }
  639.  
  640.     return this.__defaultApplicationHandler;
  641.   },
  642.  
  643.   get hasDefaultHandler() {
  644. //@line 704 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  645.     try {
  646.       if (this._shellSvc.defaultFeedReader)
  647.         return true;
  648.     }
  649.     catch(ex) {
  650.       // no default reader or _shellSvc is null
  651.     }
  652. //@line 712 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  653.  
  654.     return false;
  655.   },
  656.  
  657.   get defaultDescription() {
  658.     if (this.hasDefaultHandler)
  659.       return this._defaultApplicationHandler.name;
  660.  
  661.     // Should we instead return null?
  662.     return "";
  663.   },
  664.  
  665.   // What to do with content of this type.
  666.   get preferredAction() {
  667.     switch (this.element(this._prefSelectedAction).value) {
  668.  
  669.       case "bookmarks":
  670.         return Ci.nsIHandlerInfo.handleInternally;
  671.  
  672.       case "reader": {
  673.         let preferredApp = this.preferredApplicationHandler;
  674.         let defaultApp = this._defaultApplicationHandler;
  675.  
  676.         // If we have a valid preferred app, return useSystemDefault if it's
  677.         // the default app; otherwise return useHelperApp.
  678.         if (gApplicationsPane.isValidHandlerApp(preferredApp)) {
  679.           if (defaultApp && defaultApp.equals(preferredApp))
  680.             return Ci.nsIHandlerInfo.useSystemDefault;
  681.  
  682.           return Ci.nsIHandlerInfo.useHelperApp;
  683.         }
  684.  
  685.         // The pref is set to "reader", but we don't have a valid preferred app.
  686.         // What do we do now?  Not sure this is the best option (perhaps we
  687.         // should direct the user to the default app, if any), but for now let's
  688.         // direct the user to live bookmarks.
  689.         return Ci.nsIHandlerInfo.handleInternally;
  690.       }
  691.  
  692.       // If the action is "ask", then alwaysAskBeforeHandling will override
  693.       // the action, so it doesn't matter what we say it is, it just has to be
  694.       // something that doesn't cause the controller to hide the type.
  695.       case "ask":
  696.       default:
  697.         return Ci.nsIHandlerInfo.handleInternally;
  698.     }
  699.   },
  700.  
  701.   set preferredAction(aNewValue) {
  702.     switch (aNewValue) {
  703.  
  704.       case Ci.nsIHandlerInfo.handleInternally:
  705.         this.element(this._prefSelectedReader).value = "bookmarks";
  706.         break;
  707.  
  708.       case Ci.nsIHandlerInfo.useHelperApp:
  709.         this.element(this._prefSelectedAction).value = "reader";
  710.         // The controller has already set preferredApplicationHandler
  711.         // to the new helper app.
  712.         break;
  713.  
  714.       case Ci.nsIHandlerInfo.useSystemDefault:
  715.         this.element(this._prefSelectedAction).value = "reader";
  716.         this.preferredApplicationHandler = this._defaultApplicationHandler;
  717.         break;
  718.     }
  719.   },
  720.  
  721.   get alwaysAskBeforeHandling() {
  722.     return this.element(this._prefSelectedAction).value == "ask";
  723.   },
  724.  
  725.   set alwaysAskBeforeHandling(aNewValue) {
  726.     if (aNewValue == true)
  727.       this.element(this._prefSelectedAction).value = "ask";
  728.     else
  729.       this.element(this._prefSelectedAction).value = "reader";
  730.   },
  731.  
  732.   // Whether or not we are currently storing the action selected by the user.
  733.   // We use this to suppress notification-triggered updates to the list when
  734.   // we make changes that may spawn such updates, specifically when we change
  735.   // the action for the feed type, which results in feed preference updates,
  736.   // which spawn "pref changed" notifications that would otherwise cause us
  737.   // to rebuild the view unnecessarily.
  738.   _storingAction: false,
  739.  
  740.  
  741.   //**************************************************************************//
  742.   // nsIMIMEInfo
  743.  
  744.   get primaryExtension() {
  745.     return "xml";
  746.   },
  747.  
  748.  
  749.   //**************************************************************************//
  750.   // Storage
  751.  
  752.   // Changes to the preferred action and handler take effect immediately
  753.   // (we write them out to the preferences right as they happen),
  754.   // so we when the controller calls store() after modifying the handlers,
  755.   // the only thing we need to store is the removal of possible handlers
  756.   // XXX Should we hold off on making the changes until this method gets called?
  757.   store: function() {
  758.     for each (let app in this._possibleApplicationHandlers._removed) {
  759.       if (app instanceof Ci.nsILocalHandlerApp) {
  760.         let pref = this.element(PREF_FEED_SELECTED_APP);
  761.         var preferredAppFile = pref.value;
  762.         if (preferredAppFile) {
  763.           let preferredApp = getLocalHandlerApp(preferredAppFile);
  764.           if (app.equals(preferredApp))
  765.             pref.reset();
  766.         }
  767.       }
  768.       else {
  769.         app.QueryInterface(Ci.nsIWebContentHandlerInfo);
  770.         this._converterSvc.removeContentHandler(app.contentType, app.uri);
  771.       }
  772.     }
  773.     this._possibleApplicationHandlers._removed = [];
  774.   },
  775.  
  776.  
  777.   //**************************************************************************//
  778.   // Icons
  779.  
  780.   get smallIcon() {
  781.     return this._smallIcon;
  782.   },
  783.  
  784.   get largeIcon() {
  785.     return this._largeIcon;
  786.   }
  787.  
  788. };
  789.  
  790. var feedHandlerInfo = {
  791.   __proto__: new FeedHandlerInfo(TYPE_MAYBE_FEED),
  792.   _prefSelectedApp: PREF_FEED_SELECTED_APP, 
  793.   _prefSelectedWeb: PREF_FEED_SELECTED_WEB, 
  794.   _prefSelectedAction: PREF_FEED_SELECTED_ACTION, 
  795.   _prefSelectedReader: PREF_FEED_SELECTED_READER,
  796.   _smallIcon: "chrome://browser/skin/feeds/feedIcon16.png",
  797.   _largeIcon: "chrome://browser/skin/feeds/feedIcon.png",
  798.   _appPrefLabel: "webFeed"
  799. }
  800.  
  801. var videoFeedHandlerInfo = {
  802.   __proto__: new FeedHandlerInfo(TYPE_MAYBE_VIDEO_FEED),
  803.   _prefSelectedApp: PREF_VIDEO_FEED_SELECTED_APP, 
  804.   _prefSelectedWeb: PREF_VIDEO_FEED_SELECTED_WEB, 
  805.   _prefSelectedAction: PREF_VIDEO_FEED_SELECTED_ACTION, 
  806.   _prefSelectedReader: PREF_VIDEO_FEED_SELECTED_READER,
  807.   _smallIcon: "chrome://browser/skin/feeds/videoFeedIcon16.png",
  808.   _largeIcon: "chrome://browser/skin/feeds/videoFeedIcon.png",
  809.   _appPrefLabel: "videoPodcastFeed"
  810. }
  811.  
  812. var audioFeedHandlerInfo = {
  813.   __proto__: new FeedHandlerInfo(TYPE_MAYBE_AUDIO_FEED),
  814.   _prefSelectedApp: PREF_AUDIO_FEED_SELECTED_APP, 
  815.   _prefSelectedWeb: PREF_AUDIO_FEED_SELECTED_WEB, 
  816.   _prefSelectedAction: PREF_AUDIO_FEED_SELECTED_ACTION, 
  817.   _prefSelectedReader: PREF_AUDIO_FEED_SELECTED_READER,
  818.   _smallIcon: "chrome://browser/skin/feeds/audioFeedIcon16.png",
  819.   _largeIcon: "chrome://browser/skin/feeds/audioFeedIcon.png",
  820.   _appPrefLabel: "audioPodcastFeed"
  821. }
  822.  
  823.  
  824. //****************************************************************************//
  825. // Prefpane Controller
  826.  
  827. var gApplicationsPane = {
  828.   // The set of types the app knows how to handle.  A hash of HandlerInfoWrapper
  829.   // objects, indexed by type.
  830.   _handledTypes: {},
  831.   
  832.   // The list of types we can show, sorted by the sort column/direction.
  833.   // An array of HandlerInfoWrapper objects.  We build this list when we first
  834.   // load the data and then rebuild it when users change a pref that affects
  835.   // what types we can show or change the sort column/direction.
  836.   // Note: this isn't necessarily the list of types we *will* show; if the user
  837.   // provides a filter string, we'll only show the subset of types in this list
  838.   // that match that string.
  839.   _visibleTypes: [],
  840.  
  841.   // A count of the number of times each visible type description appears.
  842.   // We use these counts to determine whether or not to annotate descriptions
  843.   // with their types to distinguish duplicate descriptions from each other.
  844.   // A hash of integer counts, indexed by string description.
  845.   _visibleTypeDescriptionCount: {},
  846.  
  847.  
  848.   //**************************************************************************//
  849.   // Convenience & Performance Shortcuts
  850.  
  851.   // These get defined by init().
  852.   _brandShortName : null,
  853.   _prefsBundle    : null,
  854.   _list           : null,
  855.   _filter         : null,
  856.  
  857.   // Retrieve this as nsIPrefBranch and then immediately QI to nsIPrefBranch2
  858.   // so both interfaces are available to callers.
  859.   _prefSvc      : Cc["@mozilla.org/preferences-service;1"].
  860.                   getService(Ci.nsIPrefBranch).
  861.                   QueryInterface(Ci.nsIPrefBranch2),
  862.  
  863.   _mimeSvc      : Cc["@mozilla.org/mime;1"].
  864.                   getService(Ci.nsIMIMEService),
  865.  
  866.   _helperAppSvc : Cc["@mozilla.org/uriloader/external-helper-app-service;1"].
  867.                   getService(Ci.nsIExternalHelperAppService),
  868.  
  869.   _handlerSvc   : Cc["@mozilla.org/uriloader/handler-service;1"].
  870.                   getService(Ci.nsIHandlerService),
  871.  
  872.   _ioSvc        : Cc["@mozilla.org/network/io-service;1"].
  873.                   getService(Ci.nsIIOService),
  874.  
  875.  
  876.   //**************************************************************************//
  877.   // Initialization & Destruction
  878.  
  879.   init: function() {
  880.     // Initialize shortcuts to some commonly accessed elements & values.
  881.     this._brandShortName =
  882.       document.getElementById("bundleBrand").getString("brandShortName");
  883.     this._prefsBundle = document.getElementById("bundlePreferences");
  884.     this._list = document.getElementById("handlersView");
  885.     this._filter = document.getElementById("filter");
  886.  
  887.     // Observe preferences that influence what we display so we can rebuild
  888.     // the view when they change.
  889.     this._prefSvc.addObserver(PREF_SHOW_PLUGINS_IN_LIST, this, false);
  890.     this._prefSvc.addObserver(PREF_HIDE_PLUGINS_WITHOUT_EXTENSIONS, this, false);
  891.     this._prefSvc.addObserver(PREF_FEED_SELECTED_APP, this, false);
  892.     this._prefSvc.addObserver(PREF_FEED_SELECTED_WEB, this, false);
  893.     this._prefSvc.addObserver(PREF_FEED_SELECTED_ACTION, this, false);
  894.     this._prefSvc.addObserver(PREF_FEED_SELECTED_READER, this, false);
  895.  
  896.     this._prefSvc.addObserver(PREF_VIDEO_FEED_SELECTED_APP, this, false);
  897.     this._prefSvc.addObserver(PREF_VIDEO_FEED_SELECTED_WEB, this, false);
  898.     this._prefSvc.addObserver(PREF_VIDEO_FEED_SELECTED_ACTION, this, false);
  899.     this._prefSvc.addObserver(PREF_VIDEO_FEED_SELECTED_READER, this, false);
  900.  
  901.     this._prefSvc.addObserver(PREF_AUDIO_FEED_SELECTED_APP, this, false);
  902.     this._prefSvc.addObserver(PREF_AUDIO_FEED_SELECTED_WEB, this, false);
  903.     this._prefSvc.addObserver(PREF_AUDIO_FEED_SELECTED_ACTION, this, false);
  904.     this._prefSvc.addObserver(PREF_AUDIO_FEED_SELECTED_READER, this, false);
  905.  
  906.  
  907.     // Listen for window unload so we can remove our preference observers.
  908.     window.addEventListener("unload", this, false);
  909.  
  910.     // Figure out how we should be sorting the list.  We persist sort settings
  911.     // across sessions, so we can't assume the default sort column/direction.
  912.     // XXX should we be using the XUL sort service instead?
  913.     if (document.getElementById("actionColumn").hasAttribute("sortDirection")) {
  914.       this._sortColumn = document.getElementById("actionColumn");
  915.       // The typeColumn element always has a sortDirection attribute,
  916.       // either because it was persisted or because the default value
  917.       // from the xul file was used.  If we are sorting on the other
  918.       // column, we should remove it.
  919.       document.getElementById("typeColumn").removeAttribute("sortDirection");
  920.     }
  921.     else 
  922.       this._sortColumn = document.getElementById("typeColumn");
  923.  
  924.     // Load the data and build the list of handlers.
  925.     // By doing this in a timeout, we let the preferences dialog resize itself
  926.     // to an appropriate size before we add a bunch of items to the list.
  927.     // Otherwise, if there are many items, and the Applications prefpane
  928.     // is the one that gets displayed when the user first opens the dialog,
  929.     // the dialog might stretch too much in an attempt to fit them all in.
  930.     // XXX Shouldn't we perhaps just set a max-height on the richlistbox?
  931.     var _delayedPaneLoad = function(self) {
  932.       self._loadData();
  933.       self._rebuildVisibleTypes();
  934.       self._sortVisibleTypes();
  935.       self._rebuildView();
  936.  
  937.       // Notify observers that the UI is now ready
  938.       Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService).
  939.       notifyObservers(window, "app-handler-pane-loaded", null);
  940.     }
  941.     setTimeout(_delayedPaneLoad, 0, this);
  942.   },
  943.  
  944.   destroy: function() {
  945.     window.removeEventListener("unload", this, false);
  946.     this._prefSvc.removeObserver(PREF_SHOW_PLUGINS_IN_LIST, this);
  947.     this._prefSvc.removeObserver(PREF_HIDE_PLUGINS_WITHOUT_EXTENSIONS, this);
  948.     this._prefSvc.removeObserver(PREF_FEED_SELECTED_APP, this);
  949.     this._prefSvc.removeObserver(PREF_FEED_SELECTED_WEB, this);
  950.     this._prefSvc.removeObserver(PREF_FEED_SELECTED_ACTION, this);
  951.     this._prefSvc.removeObserver(PREF_FEED_SELECTED_READER, this);
  952.  
  953.     this._prefSvc.removeObserver(PREF_VIDEO_FEED_SELECTED_APP, this);
  954.     this._prefSvc.removeObserver(PREF_VIDEO_FEED_SELECTED_WEB, this);
  955.     this._prefSvc.removeObserver(PREF_VIDEO_FEED_SELECTED_ACTION, this);
  956.     this._prefSvc.removeObserver(PREF_VIDEO_FEED_SELECTED_READER, this);
  957.  
  958.     this._prefSvc.removeObserver(PREF_AUDIO_FEED_SELECTED_APP, this);
  959.     this._prefSvc.removeObserver(PREF_AUDIO_FEED_SELECTED_WEB, this);
  960.     this._prefSvc.removeObserver(PREF_AUDIO_FEED_SELECTED_ACTION, this);
  961.     this._prefSvc.removeObserver(PREF_AUDIO_FEED_SELECTED_READER, this);
  962.   },
  963.  
  964.  
  965.   //**************************************************************************//
  966.   // nsISupports
  967.  
  968.   QueryInterface: function(aIID) {
  969.     if (aIID.equals(Ci.nsIObserver) ||
  970.         aIID.equals(Ci.nsIDOMEventListener ||
  971.         aIID.equals(Ci.nsISupports)))
  972.       return this;
  973.  
  974.     throw Cr.NS_ERROR_NO_INTERFACE;
  975.   },
  976.  
  977.  
  978.   //**************************************************************************//
  979.   // nsIObserver
  980.  
  981.   observe: function (aSubject, aTopic, aData) {
  982.     // Rebuild the list when there are changes to preferences that influence
  983.     // whether or not to show certain entries in the list.
  984.     if (aTopic == "nsPref:changed" && !this._storingAction) {
  985.       // These two prefs alter the list of visible types, so we have to rebuild
  986.       // that list when they change.
  987.       if (aData == PREF_SHOW_PLUGINS_IN_LIST ||
  988.           aData == PREF_HIDE_PLUGINS_WITHOUT_EXTENSIONS) {
  989.         this._rebuildVisibleTypes();
  990.         this._sortVisibleTypes();
  991.       }
  992.  
  993.       // All the prefs we observe can affect what we display, so we rebuild
  994.       // the view when any of them changes.
  995.       this._rebuildView();
  996.     }
  997.   },
  998.  
  999.  
  1000.   //**************************************************************************//
  1001.   // nsIDOMEventListener
  1002.  
  1003.   handleEvent: function(aEvent) {
  1004.     if (aEvent.type == "unload") {
  1005.       this.destroy();
  1006.     }
  1007.   },
  1008.  
  1009.  
  1010.   //**************************************************************************//
  1011.   // Composed Model Construction
  1012.  
  1013.   _loadData: function() {
  1014.     this._loadFeedHandler();
  1015.     this._loadPluginHandlers();
  1016.     this._loadApplicationHandlers();
  1017.   },
  1018.  
  1019.   _loadFeedHandler: function() {
  1020.     this._handledTypes[TYPE_MAYBE_FEED] = feedHandlerInfo;
  1021.     feedHandlerInfo.handledOnlyByPlugin = false;
  1022.  
  1023.     this._handledTypes[TYPE_MAYBE_VIDEO_FEED] = videoFeedHandlerInfo;
  1024.     videoFeedHandlerInfo.handledOnlyByPlugin = false;
  1025.  
  1026.     this._handledTypes[TYPE_MAYBE_AUDIO_FEED] = audioFeedHandlerInfo;
  1027.     audioFeedHandlerInfo.handledOnlyByPlugin = false;
  1028.   },
  1029.  
  1030.   /**
  1031.    * Load the set of handlers defined by plugins.
  1032.    *
  1033.    * Note: if there's more than one plugin for a given MIME type, we assume
  1034.    * the last one is the one that the application will use.  That may not be
  1035.    * correct, but it's how we've been doing it for years.
  1036.    *
  1037.    * Perhaps we should instead query navigator.mimeTypes for the set of types
  1038.    * supported by the application and then get the plugin from each MIME type's
  1039.    * enabledPlugin property.  But if there's a plugin for a type, we need
  1040.    * to know about it even if it isn't enabled, since we're going to give
  1041.    * the user an option to enable it.
  1042.    * 
  1043.    * I'll also note that my reading of nsPluginTag::RegisterWithCategoryManager
  1044.    * suggests that enabledPlugin is only determined during registration
  1045.    * and does not get updated when plugin.disable_full_page_plugin_for_types
  1046.    * changes (unless modification of that preference spawns reregistration).
  1047.    * So even if we could use enabledPlugin to get the plugin that would be used,
  1048.    * we'd still need to check the pref ourselves to find out if it's enabled.
  1049.    */
  1050.   _loadPluginHandlers: function() {
  1051.     for (let i = 0; i < navigator.plugins.length; ++i) {
  1052.       let plugin = navigator.plugins[i];
  1053.       for (let j = 0; j < plugin.length; ++j) {
  1054.         let type = plugin[j].type;
  1055.  
  1056.         let handlerInfoWrapper;
  1057.         if (type in this._handledTypes)
  1058.           handlerInfoWrapper = this._handledTypes[type];
  1059.         else {
  1060.           let wrappedHandlerInfo =
  1061.             this._mimeSvc.getFromTypeAndExtension(type, null);
  1062.           handlerInfoWrapper = new HandlerInfoWrapper(type, wrappedHandlerInfo);
  1063.           handlerInfoWrapper.handledOnlyByPlugin = true;
  1064.           this._handledTypes[type] = handlerInfoWrapper;
  1065.         }
  1066.  
  1067.         handlerInfoWrapper.plugin = plugin;
  1068.       }
  1069.     }
  1070.   },
  1071.  
  1072.   /**
  1073.    * Load the set of handlers defined by the application datastore.
  1074.    */
  1075.   _loadApplicationHandlers: function() {
  1076.     var wrappedHandlerInfos = this._handlerSvc.enumerate();
  1077.     while (wrappedHandlerInfos.hasMoreElements()) {
  1078.       let wrappedHandlerInfo =
  1079.         wrappedHandlerInfos.getNext().QueryInterface(Ci.nsIHandlerInfo);
  1080.       let type = wrappedHandlerInfo.type;
  1081.  
  1082.       let handlerInfoWrapper;
  1083.       if (type in this._handledTypes)
  1084.         handlerInfoWrapper = this._handledTypes[type];
  1085.       else {
  1086.         handlerInfoWrapper = new HandlerInfoWrapper(type, wrappedHandlerInfo);
  1087.         this._handledTypes[type] = handlerInfoWrapper;
  1088.       }
  1089.  
  1090.       handlerInfoWrapper.handledOnlyByPlugin = false;
  1091.     }
  1092.   },
  1093.  
  1094.  
  1095.   //**************************************************************************//
  1096.   // View Construction
  1097.  
  1098.   _rebuildVisibleTypes: function() {
  1099.     // Reset the list of visible types and the visible type description counts.
  1100.     this._visibleTypes = [];
  1101.     this._visibleTypeDescriptionCount = {};
  1102.  
  1103.     // Get the preferences that help determine what types to show.
  1104.     var showPlugins = this._prefSvc.getBoolPref(PREF_SHOW_PLUGINS_IN_LIST);
  1105.     var hidePluginsWithoutExtensions =
  1106.       this._prefSvc.getBoolPref(PREF_HIDE_PLUGINS_WITHOUT_EXTENSIONS);
  1107.  
  1108.     for (let type in this._handledTypes) {
  1109.       let handlerInfo = this._handledTypes[type];
  1110.  
  1111.       // Hide plugins without associated extensions if so prefed so we don't
  1112.       // show a whole bunch of obscure types handled by plugins on Mac.
  1113.       // Note: though protocol types don't have extensions, we still show them;
  1114.       // the pref is only meant to be applied to MIME types, since plugins are
  1115.       // only associated with MIME types.
  1116.       // FIXME: should we also check the "suffixes" property of the plugin?
  1117.       // Filed as bug 395135.
  1118.       if (hidePluginsWithoutExtensions && handlerInfo.handledOnlyByPlugin &&
  1119.           handlerInfo.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo &&
  1120.           !handlerInfo.primaryExtension)
  1121.         continue;
  1122.  
  1123.       // Hide types handled only by plugins if so prefed.
  1124.       if (handlerInfo.handledOnlyByPlugin && !showPlugins)
  1125.         continue;
  1126.  
  1127.       // We couldn't find any reason to exclude the type, so include it.
  1128.       this._visibleTypes.push(handlerInfo);
  1129.  
  1130.       if (handlerInfo.description in this._visibleTypeDescriptionCount)
  1131.         this._visibleTypeDescriptionCount[handlerInfo.description]++;
  1132.       else
  1133.         this._visibleTypeDescriptionCount[handlerInfo.description] = 1;
  1134.     }
  1135.   },
  1136.  
  1137.   _rebuildView: function() {
  1138.     // Clear the list of entries.
  1139.     while (this._list.childNodes.length > 1)
  1140.       this._list.removeChild(this._list.lastChild);
  1141.  
  1142.     var visibleTypes = this._visibleTypes;
  1143.  
  1144.     // If the user is filtering the list, then only show matching types.
  1145.     if (this._filter.value)
  1146.       visibleTypes = visibleTypes.filter(this._matchesFilter, this);
  1147.  
  1148.     for each (let visibleType in visibleTypes) {
  1149.       let item = document.createElement("richlistitem");
  1150.       item.setAttribute("type", visibleType.type);
  1151.       item.setAttribute("typeDescription", this._describeType(visibleType));
  1152.       if (visibleType.smallIcon)
  1153.         item.setAttribute("typeIcon", visibleType.smallIcon);
  1154.       item.setAttribute("actionDescription",
  1155.                         this._describePreferredAction(visibleType));
  1156.  
  1157.       if (!this._setIconClassForPreferredAction(visibleType, item)) {
  1158.         item.setAttribute("actionIcon",
  1159.                           this._getIconURLForPreferredAction(visibleType));
  1160.       }
  1161.  
  1162.       this._list.appendChild(item);
  1163.     }
  1164.  
  1165.     this._selectLastSelectedType();
  1166.   },
  1167.  
  1168.   _matchesFilter: function(aType) {
  1169.     var filterValue = this._filter.value.toLowerCase();
  1170.     return this._describeType(aType).toLowerCase().indexOf(filterValue) != -1 ||
  1171.            this._describePreferredAction(aType).toLowerCase().indexOf(filterValue) != -1;
  1172.   },
  1173.  
  1174.   /**
  1175.    * Describe, in a human-readable fashion, the type represented by the given
  1176.    * handler info object.  Normally this is just the description provided by
  1177.    * the info object, but if more than one object presents the same description,
  1178.    * then we annotate the duplicate descriptions with the type itself to help
  1179.    * users distinguish between those types.
  1180.    *
  1181.    * @param aHandlerInfo {nsIHandlerInfo} the type being described
  1182.    * @returns {string} a description of the type
  1183.    */
  1184.   _describeType: function(aHandlerInfo) {
  1185.     if (this._visibleTypeDescriptionCount[aHandlerInfo.description] > 1)
  1186.       return this._prefsBundle.getFormattedString("typeDescriptionWithType",
  1187.                                                   [aHandlerInfo.description,
  1188.                                                    aHandlerInfo.type]);
  1189.  
  1190.     return aHandlerInfo.description;
  1191.   },
  1192.  
  1193.   /**
  1194.    * Describe, in a human-readable fashion, the preferred action to take on
  1195.    * the type represented by the given handler info object.
  1196.    *
  1197.    * XXX Should this be part of the HandlerInfoWrapper interface?  It would
  1198.    * violate the separation of model and view, but it might make more sense
  1199.    * nonetheless (f.e. it would make sortTypes easier).
  1200.    *
  1201.    * @param aHandlerInfo {nsIHandlerInfo} the type whose preferred action
  1202.    *                                      is being described
  1203.    * @returns {string} a description of the action
  1204.    */
  1205.   _describePreferredAction: function(aHandlerInfo) {
  1206.     // alwaysAskBeforeHandling overrides the preferred action, so if that flag
  1207.     // is set, then describe that behavior instead.  For most types, this is
  1208.     // the "alwaysAsk" string, but for the feed type we show something special.
  1209.     if (aHandlerInfo.alwaysAskBeforeHandling) {
  1210.       if (isFeedType(aHandlerInfo.type))
  1211.         return this._prefsBundle.getFormattedString("previewInApp",
  1212.                                                     [this._brandShortName]);
  1213.       else
  1214.         return this._prefsBundle.getString("alwaysAsk");
  1215.     }
  1216.  
  1217.     switch (aHandlerInfo.preferredAction) {
  1218.       case Ci.nsIHandlerInfo.saveToDisk:
  1219.         return this._prefsBundle.getString("saveFile");
  1220.  
  1221.       case Ci.nsIHandlerInfo.useHelperApp:
  1222.         var preferredApp = aHandlerInfo.preferredApplicationHandler;
  1223.         var name;
  1224.         if (preferredApp instanceof Ci.nsILocalHandlerApp)
  1225.           name = getDisplayNameForFile(preferredApp.executable);
  1226.         else
  1227.           name = preferredApp.name;
  1228.         return this._prefsBundle.getFormattedString("useApp", [name]);
  1229.  
  1230.       case Ci.nsIHandlerInfo.handleInternally:
  1231.         // For the feed type, handleInternally means live bookmarks.
  1232.         if (isFeedType(aHandlerInfo.type)) 
  1233.           return this._prefsBundle.getFormattedString("addLiveBookmarksInApp",
  1234.                                                       [this._brandShortName]);
  1235.  
  1236.         // For other types, handleInternally looks like either useHelperApp
  1237.         // or useSystemDefault depending on whether or not there's a preferred
  1238.         // handler app.
  1239.         if (this.isValidHandlerApp(aHandlerInfo.preferredApplicationHandler))
  1240.           return aHandlerInfo.preferredApplicationHandler.name;
  1241.  
  1242.         return aHandlerInfo.defaultDescription;
  1243.  
  1244.         // XXX Why don't we say the app will handle the type internally?
  1245.         // Is it because the app can't actually do that?  But if that's true,
  1246.         // then why would a preferredAction ever get set to this value
  1247.         // in the first place?
  1248.  
  1249.       case Ci.nsIHandlerInfo.useSystemDefault:
  1250.         return this._prefsBundle.getFormattedString("useDefault",
  1251.                                                     [aHandlerInfo.defaultDescription]);
  1252.  
  1253.       case kActionUsePlugin:
  1254.         return this._prefsBundle.getFormattedString("usePluginIn",
  1255.                                                     [aHandlerInfo.plugin.name,
  1256.                                                      this._brandShortName]);
  1257.     }
  1258.   },
  1259.  
  1260.   _selectLastSelectedType: function() {
  1261.     // If the list is disabled by the pref.downloads.disable_button.edit_actions
  1262.     // preference being locked, then don't select the type, as that would cause
  1263.     // it to appear selected, with a different background and an actions menu
  1264.     // that makes it seem like you can choose an action for the type.
  1265.     if (this._list.disabled)
  1266.       return;
  1267.  
  1268.     var lastSelectedType = this._list.getAttribute("lastSelectedType");
  1269.     if (!lastSelectedType)
  1270.       return;
  1271.  
  1272.     var item = this._list.getElementsByAttribute("type", lastSelectedType)[0];
  1273.     if (!item)
  1274.       return;
  1275.  
  1276.     this._list.selectedItem = item;
  1277.   },
  1278.  
  1279.   /**
  1280.    * Whether or not the given handler app is valid.
  1281.    *
  1282.    * @param aHandlerApp {nsIHandlerApp} the handler app in question
  1283.    *
  1284.    * @returns {boolean} whether or not it's valid
  1285.    */
  1286.   isValidHandlerApp: function(aHandlerApp) {
  1287.     if (!aHandlerApp)
  1288.       return false;
  1289.  
  1290.     if (aHandlerApp instanceof Ci.nsILocalHandlerApp)
  1291.       return this._isValidHandlerExecutable(aHandlerApp.executable);
  1292.  
  1293.     if (aHandlerApp instanceof Ci.nsIWebHandlerApp)
  1294.       return aHandlerApp.uriTemplate;
  1295.  
  1296.     if (aHandlerApp instanceof Ci.nsIWebContentHandlerInfo)
  1297.       return aHandlerApp.uri;
  1298.  
  1299.     return false;
  1300.   },
  1301.  
  1302.   _isValidHandlerExecutable: function(aExecutable) {
  1303.     return aExecutable &&
  1304.            aExecutable.exists() &&
  1305.            aExecutable.isExecutable() &&
  1306. // XXXben - we need to compare this with the running instance executable
  1307. //          just don't know how to do that via script...
  1308. // XXXmano TBD: can probably add this to nsIShellService
  1309. //@line 1369 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  1310.    aExecutable.leafName != "firefox.exe";
  1311. //@line 1377 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  1312.   },
  1313.  
  1314.   /**
  1315.    * Rebuild the actions menu for the selected entry.  Gets called by
  1316.    * the richlistitem constructor when an entry in the list gets selected.
  1317.    */
  1318.   rebuildActionsMenu: function() {
  1319.     var typeItem = this._list.selectedItem;
  1320.     var handlerInfo = this._handledTypes[typeItem.type];
  1321.     var menu =
  1322.       document.getAnonymousElementByAttribute(typeItem, "class", "actionsMenu");
  1323.     var menuPopup = menu.menupopup;
  1324.  
  1325.     // Clear out existing items.
  1326.     while (menuPopup.hasChildNodes())
  1327.       menuPopup.removeChild(menuPopup.lastChild);
  1328.  
  1329.     {
  1330.       var askMenuItem = document.createElement("menuitem");
  1331.       askMenuItem.setAttribute("alwaysAsk", "true");
  1332.       let label;
  1333.       if (isFeedType(handlerInfo.type))
  1334.         label = this._prefsBundle.getFormattedString("previewInApp",
  1335.                                                      [this._brandShortName]);
  1336.       else
  1337.         label = this._prefsBundle.getString("alwaysAsk");
  1338.       askMenuItem.setAttribute("label", label);
  1339.       askMenuItem.setAttribute("tooltiptext", label);
  1340.       askMenuItem.setAttribute(APP_ICON_ATTR_NAME, "ask");
  1341.       menuPopup.appendChild(askMenuItem);
  1342.     }
  1343.  
  1344.     // Create a menu item for saving to disk.
  1345.     // Note: this option isn't available to protocol types, since we don't know
  1346.     // what it means to save a URL having a certain scheme to disk, nor is it
  1347.     // available to feeds, since the feed code doesn't implement the capability.
  1348.     if ((handlerInfo.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo) &&
  1349.         !isFeedType(handlerInfo.type)) {
  1350.       var saveMenuItem = document.createElement("menuitem");
  1351.       saveMenuItem.setAttribute("action", Ci.nsIHandlerInfo.saveToDisk);
  1352.       let label = this._prefsBundle.getString("saveFile");
  1353.       saveMenuItem.setAttribute("label", label);
  1354.       saveMenuItem.setAttribute("tooltiptext", label);
  1355.       saveMenuItem.setAttribute(APP_ICON_ATTR_NAME, "save");
  1356.       menuPopup.appendChild(saveMenuItem);
  1357.     }
  1358.  
  1359.     // If this is the feed type, add a Live Bookmarks item.
  1360.     if (isFeedType(handlerInfo.type)) {
  1361.       var internalMenuItem = document.createElement("menuitem");
  1362.       internalMenuItem.setAttribute("action", Ci.nsIHandlerInfo.handleInternally);
  1363.       let label = this._prefsBundle.getFormattedString("addLiveBookmarksInApp",
  1364.                                                        [this._brandShortName]);
  1365.       internalMenuItem.setAttribute("label", label);
  1366.       internalMenuItem.setAttribute("tooltiptext", label);
  1367.       internalMenuItem.setAttribute(APP_ICON_ATTR_NAME, "feed");
  1368.       menuPopup.appendChild(internalMenuItem);
  1369.     }
  1370.  
  1371.     // Add a separator to distinguish these items from the helper app items
  1372.     // that follow them.
  1373.     let menuItem = document.createElement("menuseparator");
  1374.     menuPopup.appendChild(menuItem);
  1375.  
  1376.     // Create a menu item for the OS default application, if any.
  1377.     if (handlerInfo.hasDefaultHandler) {
  1378.       var defaultMenuItem = document.createElement("menuitem");
  1379.       defaultMenuItem.setAttribute("action", Ci.nsIHandlerInfo.useSystemDefault);
  1380.       let label = this._prefsBundle.getFormattedString("useDefault",
  1381.                                                        [handlerInfo.defaultDescription]);
  1382.       defaultMenuItem.setAttribute("label", label);
  1383.       defaultMenuItem.setAttribute("tooltiptext", handlerInfo.defaultDescription);
  1384.       defaultMenuItem.setAttribute("image", this._getIconURLForSystemDefault(handlerInfo));
  1385.  
  1386.       menuPopup.appendChild(defaultMenuItem);
  1387.     }
  1388.  
  1389.     // Create menu items for possible handlers.
  1390.     let preferredApp = handlerInfo.preferredApplicationHandler;
  1391.     let possibleApps = handlerInfo.possibleApplicationHandlers.enumerate();
  1392.     var possibleAppMenuItems = [];
  1393.     while (possibleApps.hasMoreElements()) {
  1394.       let possibleApp = possibleApps.getNext();
  1395.       if (!this.isValidHandlerApp(possibleApp))
  1396.         continue;
  1397.  
  1398.       let menuItem = document.createElement("menuitem");
  1399.       menuItem.setAttribute("action", Ci.nsIHandlerInfo.useHelperApp);
  1400.       let label;
  1401.       if (possibleApp instanceof Ci.nsILocalHandlerApp)
  1402.         label = getDisplayNameForFile(possibleApp.executable);
  1403.       else
  1404.         label = possibleApp.name;
  1405.       label = this._prefsBundle.getFormattedString("useApp", [label]);
  1406.       menuItem.setAttribute("label", label);
  1407.       menuItem.setAttribute("tooltiptext", label);
  1408.       menuItem.setAttribute("image", this._getIconURLForHandlerApp(possibleApp));
  1409.  
  1410.       // Attach the handler app object to the menu item so we can use it
  1411.       // to make changes to the datastore when the user selects the item.
  1412.       menuItem.handlerApp = possibleApp;
  1413.  
  1414.       menuPopup.appendChild(menuItem);
  1415.       possibleAppMenuItems.push(menuItem);
  1416.     }
  1417.  
  1418.     // Create a menu item for the plugin.
  1419.     if (handlerInfo.plugin) {
  1420.       var pluginMenuItem = document.createElement("menuitem");
  1421.       pluginMenuItem.setAttribute("action", kActionUsePlugin);
  1422.       let label = this._prefsBundle.getFormattedString("usePluginIn",
  1423.                                                        [handlerInfo.plugin.name,
  1424.                                                         this._brandShortName]);
  1425.       pluginMenuItem.setAttribute("label", label);
  1426.       pluginMenuItem.setAttribute("tooltiptext", label);
  1427.       pluginMenuItem.setAttribute(APP_ICON_ATTR_NAME, "plugin");
  1428.       menuPopup.appendChild(pluginMenuItem);
  1429.     }
  1430.  
  1431.     // Create a menu item for selecting a local application.
  1432. //@line 1498 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  1433.     // On Windows, selecting an application to open another application
  1434.     // would be meaningless so we special case executables.
  1435.     var executableType = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService)
  1436.                                                   .getTypeFromExtension("exe");
  1437.     if (handlerInfo.type != executableType)
  1438. //@line 1504 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  1439.     {
  1440.       let menuItem = document.createElement("menuitem");
  1441.       menuItem.setAttribute("oncommand", "gApplicationsPane.chooseApp(event)");
  1442.       let label = this._prefsBundle.getString("useOtherApp");
  1443.       menuItem.setAttribute("label", label);
  1444.       menuItem.setAttribute("tooltiptext", label);
  1445.       menuPopup.appendChild(menuItem);
  1446.     }
  1447.  
  1448.     // Create a menu item for managing applications.
  1449.     if (possibleAppMenuItems.length) {
  1450.       let menuItem = document.createElement("menuseparator");
  1451.       menuPopup.appendChild(menuItem);
  1452.       menuItem = document.createElement("menuitem");
  1453.       menuItem.setAttribute("oncommand", "gApplicationsPane.manageApp(event)");
  1454.       menuItem.setAttribute("label", this._prefsBundle.getString("manageApp"));
  1455.       menuPopup.appendChild(menuItem);
  1456.     }
  1457.  
  1458.     // Select the item corresponding to the preferred action.  If the always
  1459.     // ask flag is set, it overrides the preferred action.  Otherwise we pick
  1460.     // the item identified by the preferred action (when the preferred action
  1461.     // is to use a helper app, we have to pick the specific helper app item).
  1462.     if (handlerInfo.alwaysAskBeforeHandling)
  1463.       menu.selectedItem = askMenuItem;
  1464.     else switch (handlerInfo.preferredAction) {
  1465.       case Ci.nsIHandlerInfo.handleInternally:
  1466.         menu.selectedItem = internalMenuItem;
  1467.         break;
  1468.       case Ci.nsIHandlerInfo.useSystemDefault:
  1469.         menu.selectedItem = defaultMenuItem;
  1470.         break;
  1471.       case Ci.nsIHandlerInfo.useHelperApp:
  1472.         if (preferredApp)
  1473.           menu.selectedItem = 
  1474.             possibleAppMenuItems.filter(function(v) v.handlerApp.equals(preferredApp))[0];
  1475.         break;
  1476.       case kActionUsePlugin:
  1477.         menu.selectedItem = pluginMenuItem;
  1478.         break;
  1479.       case Ci.nsIHandlerInfo.saveToDisk:
  1480.         menu.selectedItem = saveMenuItem;
  1481.         break;
  1482.     }
  1483.   },
  1484.  
  1485.  
  1486.   //**************************************************************************//
  1487.   // Sorting & Filtering
  1488.  
  1489.   _sortColumn: null,
  1490.  
  1491.   /**
  1492.    * Sort the list when the user clicks on a column header.
  1493.    */
  1494.   sort: function (event) {
  1495.     var column = event.target;
  1496.  
  1497.     // If the user clicked on a new sort column, remove the direction indicator
  1498.     // from the old column.
  1499.     if (this._sortColumn && this._sortColumn != column)
  1500.       this._sortColumn.removeAttribute("sortDirection");
  1501.  
  1502.     this._sortColumn = column;
  1503.  
  1504.     // Set (or switch) the sort direction indicator.
  1505.     if (column.getAttribute("sortDirection") == "ascending")
  1506.       column.setAttribute("sortDirection", "descending");
  1507.     else
  1508.       column.setAttribute("sortDirection", "ascending");
  1509.  
  1510.     this._sortVisibleTypes();
  1511.     this._rebuildView();
  1512.   },
  1513.  
  1514.   /**
  1515.    * Sort the list of visible types by the current sort column/direction.
  1516.    */
  1517.   _sortVisibleTypes: function() {
  1518.     if (!this._sortColumn)
  1519.       return;
  1520.  
  1521.     var t = this;
  1522.  
  1523.     function sortByType(a, b) {
  1524.       return t._describeType(a).toLowerCase().
  1525.              localeCompare(t._describeType(b).toLowerCase());
  1526.     }
  1527.  
  1528.     function sortByAction(a, b) {
  1529.       return t._describePreferredAction(a).toLowerCase().
  1530.              localeCompare(t._describePreferredAction(b).toLowerCase());
  1531.     }
  1532.  
  1533.     switch (this._sortColumn.getAttribute("value")) {
  1534.       case "type":
  1535.         this._visibleTypes.sort(sortByType);
  1536.         break;
  1537.       case "action":
  1538.         this._visibleTypes.sort(sortByAction);
  1539.         break;
  1540.     }
  1541.  
  1542.     if (this._sortColumn.getAttribute("sortDirection") == "descending")
  1543.       this._visibleTypes.reverse();
  1544.   },
  1545.  
  1546.   /**
  1547.    * Filter the list when the user enters a filter term into the filter field.
  1548.    */
  1549.   filter: function() {
  1550.     this._rebuildView();
  1551.   },
  1552.  
  1553.   focusFilterBox: function() {
  1554.     this._filter.focus();
  1555.     this._filter.select();
  1556.   },
  1557.  
  1558.  
  1559.   //**************************************************************************//
  1560.   // Changes
  1561.  
  1562.   onSelectAction: function(aActionItem) {
  1563.     this._storingAction = true;
  1564.  
  1565.     try {
  1566.       this._storeAction(aActionItem);
  1567.     }
  1568.     finally {
  1569.       this._storingAction = false;
  1570.     }
  1571.   },
  1572.  
  1573.   _storeAction: function(aActionItem) {
  1574.     var typeItem = this._list.selectedItem;
  1575.     var handlerInfo = this._handledTypes[typeItem.type];
  1576.  
  1577.     if (aActionItem.hasAttribute("alwaysAsk")) {
  1578.       handlerInfo.alwaysAskBeforeHandling = true;
  1579.     }
  1580.     else if (aActionItem.hasAttribute("action")) {
  1581.       let action = parseInt(aActionItem.getAttribute("action"));
  1582.  
  1583.       // Set the plugin state if we're enabling or disabling a plugin.
  1584.       if (action == kActionUsePlugin)
  1585.         handlerInfo.enablePluginType();
  1586.       else if (handlerInfo.plugin && !handlerInfo.isDisabledPluginType)
  1587.         handlerInfo.disablePluginType();
  1588.  
  1589.       // Set the preferred application handler.
  1590.       // We leave the existing preferred app in the list when we set
  1591.       // the preferred action to something other than useHelperApp so that
  1592.       // legacy datastores that don't have the preferred app in the list
  1593.       // of possible apps still include the preferred app in the list of apps
  1594.       // the user can choose to handle the type.
  1595.       if (action == Ci.nsIHandlerInfo.useHelperApp)
  1596.         handlerInfo.preferredApplicationHandler = aActionItem.handlerApp;
  1597.  
  1598.       // Set the "always ask" flag.
  1599.       handlerInfo.alwaysAskBeforeHandling = false;
  1600.  
  1601.       // Set the preferred action.
  1602.       handlerInfo.preferredAction = action;
  1603.     }
  1604.  
  1605.     handlerInfo.store();
  1606.  
  1607.     // Make sure the handler info object is flagged to indicate that there is
  1608.     // now some user configuration for the type.
  1609.     handlerInfo.handledOnlyByPlugin = false;
  1610.  
  1611.     // Update the action label and image to reflect the new preferred action.
  1612.     typeItem.setAttribute("actionDescription",
  1613.                           this._describePreferredAction(handlerInfo));
  1614.     if (!this._setIconClassForPreferredAction(handlerInfo, typeItem)) {
  1615.       typeItem.setAttribute("actionIcon",
  1616.                             this._getIconURLForPreferredAction(handlerInfo));
  1617.     }
  1618.   },
  1619.  
  1620.   manageApp: function(aEvent) {
  1621.     // Don't let the normal "on select action" handler get this event,
  1622.     // as we handle it specially ourselves.
  1623.     aEvent.stopPropagation();
  1624.  
  1625.     var typeItem = this._list.selectedItem;
  1626.     var handlerInfo = this._handledTypes[typeItem.type];
  1627.  
  1628.     document.documentElement.openSubDialog("chrome://browser/content/preferences/applicationManager.xul",
  1629.                                            "", handlerInfo);
  1630.  
  1631.     // Rebuild the actions menu so that we revert to the previous selection,
  1632.     // or "Always ask" if the previous default application has been removed
  1633.     this.rebuildActionsMenu();
  1634.  
  1635.     // update the richlistitem too. Will be visible when selecting another row
  1636.     typeItem.setAttribute("actionDescription",
  1637.                           this._describePreferredAction(handlerInfo));
  1638.     if (!this._setIconClassForPreferredAction(handlerInfo, typeItem)) {
  1639.       typeItem.setAttribute("actionIcon",
  1640.                             this._getIconURLForPreferredAction(handlerInfo));
  1641.     }
  1642.   },
  1643.  
  1644.   chooseApp: function(aEvent) {
  1645.     // Don't let the normal "on select action" handler get this event,
  1646.     // as we handle it specially ourselves.
  1647.     aEvent.stopPropagation();
  1648.  
  1649.     var handlerApp;
  1650.  
  1651. //@line 1717 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  1652.     var params = {};
  1653.     var handlerInfo = this._handledTypes[this._list.selectedItem.type];
  1654.  
  1655.     if (isFeedType(handlerInfo.type)) {
  1656.       // MIME info will be null, create a temp object.
  1657.       params.mimeInfo = this._mimeSvc.getFromTypeAndExtension(handlerInfo.type, 
  1658.                                                  handlerInfo.primaryExtension);
  1659.     } else {
  1660.       params.mimeInfo = handlerInfo.wrappedHandlerInfo;
  1661.     }
  1662.  
  1663.     params.title         = this._prefsBundle.getString("fpTitleChooseApp");
  1664.     params.description   = handlerInfo.description;
  1665.     params.filename      = null;
  1666.     params.handlerApp    = null;
  1667.  
  1668.     window.openDialog("chrome://global/content/appPicker.xul", null,
  1669.                       "chrome,modal,centerscreen,titlebar,dialog=yes",
  1670.                       params);
  1671.  
  1672.     if (params.handlerApp && 
  1673.         params.handlerApp.executable && 
  1674.         params.handlerApp.executable.isFile()) {
  1675.       handlerApp = params.handlerApp;
  1676.  
  1677.       // Add the app to the type's list of possible handlers.
  1678.       handlerInfo.addPossibleApplicationHandler(handlerApp);
  1679.     }
  1680. //@line 1765 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\applications.js"
  1681.  
  1682.     // Rebuild the actions menu whether the user picked an app or canceled.
  1683.     // If they picked an app, we want to add the app to the menu and select it.
  1684.     // If they canceled, we want to go back to their previous selection.
  1685.     this.rebuildActionsMenu();
  1686.  
  1687.     // If the user picked a new app from the menu, select it.
  1688.     if (handlerApp) {
  1689.       let typeItem = this._list.selectedItem;
  1690.       let actionsMenu =
  1691.         document.getAnonymousElementByAttribute(typeItem, "class", "actionsMenu");
  1692.       let menuItems = actionsMenu.menupopup.childNodes;
  1693.       for (let i = 0; i < menuItems.length; i++) {
  1694.         let menuItem = menuItems[i];
  1695.         if (menuItem.handlerApp && menuItem.handlerApp.equals(handlerApp)) {
  1696.           actionsMenu.selectedIndex = i;
  1697.           this.onSelectAction(menuItem);
  1698.           break;
  1699.         }
  1700.       }
  1701.     }
  1702.   },
  1703.  
  1704.   // Mark which item in the list was last selected so we can reselect it
  1705.   // when we rebuild the list or when the user returns to the prefpane.
  1706.   onSelectionChanged: function() {
  1707.     if (this._list.selectedItem)
  1708.       this._list.setAttribute("lastSelectedType",
  1709.                               this._list.selectedItem.getAttribute("type"));
  1710.   },
  1711.  
  1712.   _setIconClassForPreferredAction: function(aHandlerInfo, aElement) {
  1713.     // If this returns true, the attribute that CSS sniffs for was set to something
  1714.     // so you shouldn't manually set an icon URI.
  1715.     // This removes the existing actionIcon attribute if any, even if returning false.
  1716.     aElement.removeAttribute("actionIcon");
  1717.  
  1718.     if (aHandlerInfo.alwaysAskBeforeHandling) {
  1719.       aElement.setAttribute(APP_ICON_ATTR_NAME, "ask");
  1720.       return true;
  1721.     }
  1722.  
  1723.     switch (aHandlerInfo.preferredAction) {
  1724.       case Ci.nsIHandlerInfo.saveToDisk:
  1725.         aElement.setAttribute(APP_ICON_ATTR_NAME, "save");
  1726.         return true;
  1727.  
  1728.       case Ci.nsIHandlerInfo.handleInternally:
  1729.         if (isFeedType(aHandlerInfo.type)) {
  1730.           aElement.setAttribute(APP_ICON_ATTR_NAME, "feed");
  1731.           return true;
  1732.         }
  1733.         break;
  1734.  
  1735.       case kActionUsePlugin:
  1736.         aElement.setAttribute(APP_ICON_ATTR_NAME, "plugin");
  1737.         return true;
  1738.     }
  1739.     aElement.removeAttribute(APP_ICON_ATTR_NAME);
  1740.     return false;
  1741.   },
  1742.  
  1743.   _getIconURLForPreferredAction: function(aHandlerInfo) {
  1744.     switch (aHandlerInfo.preferredAction) {
  1745.       case Ci.nsIHandlerInfo.useSystemDefault:
  1746.         return this._getIconURLForSystemDefault(aHandlerInfo);
  1747.  
  1748.       case Ci.nsIHandlerInfo.useHelperApp:
  1749.         let (preferredApp = aHandlerInfo.preferredApplicationHandler) {
  1750.           if (this.isValidHandlerApp(preferredApp))
  1751.             return this._getIconURLForHandlerApp(preferredApp);
  1752.         }
  1753.         break;
  1754.  
  1755.       // This should never happen, but if preferredAction is set to some weird
  1756.       // value, then fall back to the generic application icon.
  1757.       default:
  1758.         return ICON_URL_APP;
  1759.     }
  1760.   },
  1761.  
  1762.   _getIconURLForHandlerApp: function(aHandlerApp) {
  1763.     if (aHandlerApp instanceof Ci.nsILocalHandlerApp)
  1764.       return this._getIconURLForFile(aHandlerApp.executable);
  1765.  
  1766.     if (aHandlerApp instanceof Ci.nsIWebHandlerApp)
  1767.       return this._getIconURLForWebApp(aHandlerApp.uriTemplate);
  1768.  
  1769.     if (aHandlerApp instanceof Ci.nsIWebContentHandlerInfo)
  1770.       return this._getIconURLForWebApp(aHandlerApp.uri)
  1771.  
  1772.     // We know nothing about other kinds of handler apps.
  1773.     return "";
  1774.   },
  1775.  
  1776.   _getIconURLForFile: function(aFile) {
  1777.     var fph = this._ioSvc.getProtocolHandler("file").
  1778.               QueryInterface(Ci.nsIFileProtocolHandler);
  1779.     var urlSpec = fph.getURLSpecFromFile(aFile);
  1780.  
  1781.     return "moz-icon://" + urlSpec + "?size=16";
  1782.   },
  1783.  
  1784.   _getIconURLForWebApp: function(aWebAppURITemplate) {
  1785.     var uri = this._ioSvc.newURI(aWebAppURITemplate, null, null);
  1786.  
  1787.     // Unfortunately we can't use the favicon service to get the favicon,
  1788.     // because the service looks in the annotations table for a record with
  1789.     // the exact URL we give it, and users won't have such records for URLs
  1790.     // they don't visit, and users won't visit the web app's URL template,
  1791.     // they'll only visit URLs derived from that template (i.e. with %s
  1792.     // in the template replaced by the URL of the content being handled).
  1793.  
  1794.     if (/^https?/.test(uri.scheme))
  1795.       return uri.prePath + "/favicon.ico";
  1796.  
  1797.     return "";
  1798.   },
  1799.  
  1800.   _getIconURLForSystemDefault: function(aHandlerInfo) {
  1801.     // Handler info objects for MIME types on some OSes implement a property bag
  1802.     // interface from which we can get an icon for the default app, so if we're
  1803.     // dealing with a MIME type on one of those OSes, then try to get the icon.
  1804.     if ("wrappedHandlerInfo" in aHandlerInfo) {
  1805.       let wrappedHandlerInfo = aHandlerInfo.wrappedHandlerInfo;
  1806.  
  1807.       if (wrappedHandlerInfo instanceof Ci.nsIMIMEInfo &&
  1808.           wrappedHandlerInfo instanceof Ci.nsIPropertyBag) {
  1809.         try {
  1810.           let url = wrappedHandlerInfo.getProperty("defaultApplicationIconURL");
  1811.           if (url)
  1812.             return url + "?size=16";
  1813.         }
  1814.         catch(ex) {}
  1815.       }
  1816.     }
  1817.  
  1818.     // If this isn't a MIME type object on an OS that supports retrieving
  1819.     // the icon, or if we couldn't retrieve the icon for some other reason,
  1820.     // then use a generic icon.
  1821.     return ICON_URL_APP;
  1822.   }
  1823.  
  1824. };
  1825.